﻿// Write a program that can solve these tasks:
//      01. Reverses the digits of a number
//      02. Calculates the average of a sequence of integers
//      03. Solves a linear equation a * x + b = 0
// Create appropriate methods.
// Provide a simple text-based menu for the user to choose which task to solve.
// Validate the input data:
//      - The decimal number should be non-negative
//      - The sequence should not be empty
//      - "a" should not be equal to 0

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;

class SolveDifferentTasks
{
    static void Main()
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
        string title = "The program solve several task:";
        List<string> menuItems =
            new List<string>{ 
                            "Reverses the digits of a number",
                            "Calculates the average of a sequence of integers",
                            "Solves a linear equation a * x + b = 0"
                            };
        string tab = new String(' ', 4);
        PrintMenu(title, menuItems, tab);
        int choice = ReadInput(menuItems.Count);
        Console.WriteLine();
        RunTask(choice);
        Console.WriteLine();
    }

    private static void RunTask(int choice)
    {
        if (choice.Equals(1))
        {
            decimal number = InputPositiveDesimal();
            decimal reversedNumber = ReverseDecimalDigit.Reverse(number);
            Console.Write("Reversed number is: {0}", reversedNumber);
        }
        else if (choice.Equals(2))
        {
            int[] numbers = InputNumberSequence();
            //int[] numbers = { 4, 6, 73, 0, -5, 34, 6, 1 };
            decimal average = GetAverage(numbers);
            Console.WriteLine("Average of numbers sequence is: {0}", average);
        }
        else if (choice.Equals(3))
        {
            decimal a;
            do
            {
                Console.Write("Enter a = ");
            } while (!decimal.TryParse(Console.ReadLine().Replace(",", "."), out a) || a == 0);

            decimal b;
            do
            {
                Console.Write("Enter b = ");
            } while (!decimal.TryParse(Console.ReadLine().Replace(",", "."), out b));
            decimal result = SolveLinearEquation(a, b);
            char sign = '+';
            if (b < 0)
            {
                sign = '-';
            }
            Console.WriteLine("{0} * x {1} {2} = 0; x = {3}", a, sign, Math.Abs(b), result);
        }
        else if (choice.Equals(4))
        {
            Console.WriteLine("Good bye.");
        }
        else
        {
            Console.WriteLine("Invalid choice.");
        }
    }

    private static void PrintMenu(string title, List<string> menuItem, string tab)
    {
        Console.WriteLine(title);
        Console.WriteLine(new String('-', title.Length));
        // add exit option
        menuItem.Add("Exit");
        // print menu
        for (int i = 0; i < menuItem.Count; i++)
        {
            Console.WriteLine("{0}{1}. {2}", tab, i + 1, menuItem[i]); //TODO: {0} -> 01 instead of 1
        }
        Console.WriteLine();
    }

    private static int ReadInput(int itemCount)
    {
        int choice = 0;
        do
        {
            Console.Write("Choice number of the task: ");
        } while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > itemCount);

        return choice;
    }

    private static decimal InputPositiveDesimal()
    {
        decimal number;
        do
        {
            Console.Write("Enter positive decimal number: ");
        } while (!decimal.TryParse(Console.ReadLine().Replace(",", "."), out number) || !IsPositive(number));

        return number;
    }

    private static bool IsPositive(decimal number)
    {
        bool positive = false;
        if (number > 0)
        {
            positive = true;
        }

        return positive;
    }

    private static int[] InputNumberSequence()
    {
        int count = 0;
        do
        {
            Console.Write("Enter count of numbers: ");
        } while (!int.TryParse(Console.ReadLine(), out count) || count <= 0);
        int[] numbers = new int[count];
        for (int i = 0; i < count; i++)
        {
            do
            {
                Console.Write("Enter number {0}: ", i + 1);
            } while (!int.TryParse(Console.ReadLine(), out numbers[i]));
        }

        return numbers;
    }

    private static decimal GetAverage(int[] numbers)
    {
        int count = numbers.Length;
        int sum = 0;
        foreach (var item in numbers)
        {
            sum += item;
        }

        return (decimal)sum / count;
    }

    private static decimal SolveLinearEquation(decimal a, decimal b)
    {
        return -b / a;
    }
}